<?php

namespace addon\lian\model\sqlmodel;

use think\Model;

/**
 * 链动等级模型
 */
class LianLevelModel extends Model
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'level_id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'lian_level';

    /**
     * 获取分页列表
     * @param array $condition
     * @param int $page
     * @param int $page_size
     * @param string $order
     * @param string $field
     * @return \think\Paginator
     */
    public function getPage($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
    {
        $order = !empty($order) ? $order : 'level_num asc';
        $list = $this->where($condition)
            ->field($field)
            ->order($order)
            ->paginate([
                'list_rows' => $page_size,
                'page' => $page
            ]);
        
        return [
            'code' => 0,
            'message' => 'SUCCESS',
            'data' => [
                'count' => $list->total(),
                'list' => $list->items(),
                'page' => $page,
                'page_size' => $page_size
            ]
        ];
    }
    
    /**
     * 获取链动等级列表
     * @param array $condition
     * @param string $field
     * @param string $order
     * @param string $limit
     * @return array
     */
    public function getLevelList($condition = [], $field = '*', $order = '', $limit = null)
    {
        $order = !empty($order) ? $order : 'level_num asc';
        $list = $this->where($condition)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select()
            ->toArray();
            
        return $list;
    }
    
    /**
     * 获取链动等级信息
     * @param array $condition
     * @param string $field
     * @return array
     */
    public function getLevelInfo($condition = [], $field = '*')
    {
        $info = $this->where($condition)->field($field)->find();
        return !empty($info) ? $info->toArray() : [];
    }
}
